package src.origintrail;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.*;
import src.origintrail.Player.Pace;
import src.origintrail.Player.Rations;

/**
 * The starting text generator.
 * @author csmythe
 */
public class ModuleStart {

    private ClassList c;
    private int minFollowers = 0;
    private int maxFollowers = 4;
    private JPanel[] followerPanel;
    private JLabel[] instructions;
    private JTextField nameBox;
    private JTextField[] followerBox;
    private JCheckBox[] allowFollower;
    private JComboBox professionBox;
    private JComboBox paceBox;
    private JComboBox rationsBox;
    private JButton nextButton;
    private static boolean isInitialized = false;
    
    /**
     * Constructor
     */
    public ModuleStart(){
        c = new ClassList();
    }
    
    /**
     * Runs the starting actions
     * @throws Exception This may fail if mistakes are made in other files
     */
    public void run() throws Exception{
        if(isInitialized)
            OriginTrail.startPanel.removeAll();
        isInitialized = true;
        OriginTrail.animPanel.setVisible(false);
        OriginTrail.uiPanel.setVisible(false);
        OriginTrail.shopPanel.setVisible(false);
        OriginTrail.startPanel.setLayout(new BoxLayout(OriginTrail.startPanel, BoxLayout.Y_AXIS));
        System.out.print("Welcome to the Origin Trail\n");
//        instructions = new JLabel[8];
        //for(JLabel l : instructions)
//            l = new JLabel();
        nameBox = new JTextField("Type your name here");
        nameBox.setPreferredSize(new Dimension(200, 18));
        followerPanel = new JPanel[maxFollowers];
        followerBox = new JTextField[maxFollowers];
        allowFollower = new JCheckBox[maxFollowers];
        for(int i = 0; i < maxFollowers; i++){
            followerPanel[i] = new JPanel();
            followerBox[i] = new JTextField("Type a follower\'s name here");
            allowFollower[i] = new JCheckBox();
            followerPanel[i].setLayout(new BoxLayout(followerPanel[i], BoxLayout.X_AXIS));
            allowFollower[i].setSelected(true);
            allowFollower[i].addActionListener(new CheckListener(allowFollower[i], followerBox[i]));
            followerPanel[i].add(allowFollower[i]);
            followerPanel[i].add(followerBox[i]);
        }
        professionBox = new JComboBox(new ClassList().getClasses().toArray());
        professionBox.setSelectedItem(new ClassList().getClasses().toArray()[0]);
        paceBox = new JComboBox(Player.Pace.list);
        paceBox.setSelectedItem(Player.Pace.list[2]);
        rationsBox = new JComboBox(Player.Rations.list);
        rationsBox.setSelectedItem(Player.Rations.list[2]);
        
        nextButton = new JButton("Next");
        nextButton.setPreferredSize(new Dimension(96,24));
        nextButton.addActionListener(new NextListener(this));
        
        OriginTrail.startPanel.add(nameBox);
        for(JPanel p : followerPanel)
            OriginTrail.startPanel.add(p);
        OriginTrail.startPanel.add(professionBox);
        OriginTrail.startPanel.add(paceBox);
        OriginTrail.startPanel.add(rationsBox);
        OriginTrail.startPanel.add(nextButton);
        OriginTrail.otFrame.pack();
        OriginTrail.startPanel.setVisible(true);
        
    }
    /**
     * Asks the players name
     * @return name
     */
/*    private String askName(){
        try{
        instructions.setText("Please enter your name, then click \"next\".");
        OriginTrail.uiPanel.add(instructions, BorderLayout.NORTH);
        OriginTrail.uiPanel.add(inputBox, BorderLayout.WEST);
        OriginTrail.uiPanel.add(nextButton, BorderLayout.EAST);
        OriginTrail.otFrame.pack();
        t = new TextListener();
        nextButton.addActionListener(t);
        while(!t.set)
            nextButton.setEnabled(true);
        System.out.println(inputBox.getText());
        OriginTrail.uiPanel.remove(inputBox);
        t.set = false;
        }catch(NullPointerException n){
            n.printStackTrace();
        }
        return inputBox.getText();
    }
    /**
     * gives the player profession options
     * @return profession
     */
//    private String askProf(){
//        JRadioButton[] classesPicker = new JRadioButton[c.getClasses().size()];
//        ButtonGroup classesGroup = new ButtonGroup();
//        JPanel radioPanel = new JPanel();
//        for (int i = 0; i < c.getClasses().size(); i++){
//            classesPicker[i] = new JRadioButton(new ClassList().getEntryName(i));
//            classesGroup.add(classesPicker[i]);
//            radioPanel.add(classesPicker[i]);
//        }
//        classesPicker[0].setSelected(true);
//        OriginTrail.uiPanel.add(radioPanel, BorderLayout.WEST);
//        OriginTrail.otFrame.pack();
//        t.set = false;
//        nextButton.setEnabled(false);
//        while(!t.set){
//            nextButton.setEnabled(true);
//        }
//        JRadioButton selBut = classesPicker[0];
//        for(JRadioButton j : classesPicker)
//            if(j.isSelected())
//                selBut = j;
//        System.out.println(selBut.getText());
//        radioPanel.setVisible(false);
//        OriginTrail.uiPanel.remove(radioPanel);
//        t.set = false;
//        return selBut.getText();
//    }
//    /**
//     * asks the player the names of party members they will take
//     * @return party[]
//     */
//    private String[] askParty() {
//        int total = numberOfFollowers(minFollowers, maxFollowers);
//        String[] ret = new String[total];
//        OriginTrail.uiPanel.add(inputBox, BorderLayout.WEST);
//        
//        for (int i = 0; i < total; i++){
//            inputBox.setText("");
//            instructions.setText("\nPlease enter party member " + (i+1) + "'s name: ");
//            OriginTrail.otFrame.pack();
//            
//            while(!t.set)
//                nextButton.setEnabled(true);
//            ret[i] = inputBox.getText();
//            System.out.println(inputBox.getText());
//            t.set = false;
//        }
//        OriginTrail.uiPanel.remove(inputBox);
//        return ret;
//    }
//    /**
//     * asks for the number of party members
//     * @param min 
//     * @param max
//     * @return num
//     */
//    private int numberOfFollowers(int min, int max){
//        int i = -1;
//        ButtonGroup numGroup = new ButtonGroup();
//        instructions.setText("How many party members?");
//        JRadioButton[] nums = new JRadioButton[max-min + 1];
//        JPanel radioPanel = new JPanel();
//        radioPanel.setLayout(new BoxLayout(radioPanel, BoxLayout.X_AXIS));
//        for (int j = max; j >= min; j--){
//            nums[max-j] = new JRadioButton(String.valueOf(j));
//            numGroup.add(nums[j]);
//            radioPanel.add(nums[max-j]);
//        }
//        nums[0].setSelected(true);
//        OriginTrail.uiPanel.add(radioPanel, BorderLayout.WEST);
//        OriginTrail.otFrame.pack();
//        nextButton.setEnabled(false);
//            try{
//                while(!t.set)
//                    nextButton.setEnabled(true);
//                
//                JRadioButton selBut = nums[0];
//                for(JRadioButton j : nums)
//                    if(j.isSelected())
//                        selBut = j;
//                i = Integer.parseInt(selBut.getText());
//                System.out.println(selBut.getText());
//            }catch(Exception nme){
//                nme.printStackTrace();
//                System.out.print("Something went horribly wrong with our button-making.\nI'm sorry.\n");
//            }
//        OriginTrail.uiPanel.remove(radioPanel);
//        t.set = false;
//        return i;
//    }
//    /**
//     * gives the player pace options
//     * @return pace
//     */
//    private Pace askPace() {
//        JRadioButton[] paces = new JRadioButton[Pace.values().length];
//        instructions.setText("Which pace would you like to start at?");
//        JPanel pacePanel = new JPanel();
//        ButtonGroup pacebutton = new ButtonGroup();
//        for(int i = 0; i < paces.length; i++){
//            paces[i] = new JRadioButton(Pace.class.getEnumConstants()[i].name);
//            pacebutton.add(paces[i]);
//            pacePanel.add(paces[i]);
//        }
//        paces[2].setSelected(true);
//        OriginTrail.uiPanel.add(pacePanel, BorderLayout.WEST);
//        OriginTrail.otFrame.pack();
//        t.set = false;
//        nextButton.setEnabled(false);
//        while(!t.set){
//            nextButton.setEnabled(true);
//        }
//        JRadioButton selBut = paces[0];
//        for(JRadioButton j : paces)
//            if(j.isSelected())
//                selBut = j;
//        System.out.println(selBut.getText());
//        OriginTrail.uiPanel.remove(pacePanel);
//        t.set = false;
//        String ret = "";
//        for(int i = 0; i < paces.length; i++)
//            if(selBut.getText().equals(Pace.class.getEnumConstants()[i].name))
//                ret = Pace.class.getEnumConstants()[i].toString();
//        return Pace.valueOf(ret);
//    }
//    /**
//     * gives the player rations options
//     * @return rations
//     */
//    private Rations askRations() {
//        JRadioButton[] rations = new JRadioButton[Rations.values().length];
//        instructions.setText("Which ration level would you like to start at?");
//        JPanel rationPanel = new JPanel();
//        ButtonGroup rationbutton = new ButtonGroup();
//        for(int i = 0; i < rations.length; i++){
//            rations[i] = new JRadioButton(Rations.class.getEnumConstants()[i].name);
//            rationbutton.add(rations[i]);
//            rationPanel.add(rations[i]);
//        }
//        rations[2].setSelected(true);
//        OriginTrail.uiPanel.add(rationPanel, BorderLayout.WEST);
//        OriginTrail.otFrame.pack();
//        t.set = false;
//        nextButton.setEnabled(false);
//        while(!t.set){
//            nextButton.setEnabled(true);
//        }
//        JRadioButton selBut = rations[0];
//        for(JRadioButton j : rations)
//            if(j.isSelected())
//                selBut = j;
//        System.out.println(selBut.getText());
//        OriginTrail.uiPanel.remove(rationPanel);
//        t.set = false;
//        String ret = "";
//        for(int i = 0; i < rations.length; i++)
//            if(selBut.getText().equals(Rations.class.getEnumConstants()[i].name))
//                ret = Rations.class.getEnumConstants()[i].toString();
//        return Rations.valueOf(ret);
//    }
//    /**
//     * waits for user input
//     * @author csmythe
//     *
//     */
//    private class TextListener implements ActionListener{
//        public boolean set = false;
//
//        @Override
//        public void actionPerformed(ActionEvent e) {
//            set = true;
//        }
//    
//    }
    private static class NextListener implements ActionListener {
        ModuleStart base;

        public NextListener(ModuleStart m) {
            base = m;
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            boolean allow = true;
            String name = "";
            ArrayList<String> followers = new ArrayList<String>();
            
            if(!"".equals(base.nameBox.getText()))
                name = base.nameBox.getText();
            else
                allow = false;
            
            for(JTextField f : base.followerBox)
                if(!"".equals(f.getText()))
                    followers.add(f.getText());
                else
                    allow = false;
            
            if(allow){
                String profession = ((String)(base.professionBox.getSelectedItem()));
                Pace p = Pace.values()[base.paceBox.getSelectedIndex()];
                Rations r = Rations.values()[base.rationsBox.getSelectedIndex()];
                
                try{
                    OriginTrail.plr = (Player)Class.forName("src.origintrail." + profession).newInstance();
                }catch(Exception x){
                    x.printStackTrace();
                }
                
                String[] party = new String[followers.size()];
                for(int i = 0; i < party.length; i++)
                    party[i] = followers.remove(0);
                
                OriginTrail.plr.initialize(name, party, p, r);
                
                EvtSpawner.instance.spawnShopEvent();
                OriginTrail.startPanel.setVisible(false);
                try{
                    EvtSpawner.instance.getHandler().pop().activate();
                }catch(Exception x){
                    x.printStackTrace();
                }
            }
        }
    }
    
    private static class CheckListener implements ActionListener{
        JCheckBox switcher;
        JTextField connection;
        
        public CheckListener(JCheckBox s, JTextField j){
            switcher = s;
            connection = j;
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            connection.setEnabled(switcher.isSelected());
        }
        
    }
}
